﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Testharness
{
    public partial class EIBDSymbolListGeneratorForm : Form
    {
        public EIBDSymbolListGeneratorForm()
        {
            InitializeComponent();
        }

        private void _SymbolsListBox_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (files != null && files.Length > 0)
            {
                string filename = files[0];

                using (StreamReader sr = new StreamReader(filename))
                {
                    //make sure we havent reached the end
                    while (sr.Peek() >= 0)
                    {
                        //read the symbol line
                        string text = sr.ReadLine();

                        string[] items = text.Split(new char[] { ' ' });

                        string[] items2 = items.Where(i => AllUpperCase(i)).Where(i => i != "NH").ToArray();


                        string symbol = items2[items2.Length-1];

                        //does the list box contain the symbol
                        if (!_SymbolsListBox.Items.Contains(symbol))
                        {
                            try
                            {
                                _SymbolsListBox.Items.Add(symbol);
                            }
                            catch (Exception exception)
                            {
                                MessageBox.Show(exception.ToString());
                            }
                        }
                    }
                }

                //sort the symbols
                List<string> symbolsList = new List<string>();

                foreach (string symbol in _SymbolsListBox.Items)
                {
                    symbolsList.Add(symbol);
                }

                symbolsList.Sort();

                _SymbolsListBox.Items.Clear();

                foreach (string symbol in symbolsList)
                {
                    _SymbolsListBox.Items.Add(symbol);
                }

                _SymbolsCountLabel.Text = _SymbolsCountLabel.Text =
                    string.Format("{0} symbols", _SymbolsListBox.Items.Count);
            }
        }

        private void _SymbolsListBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                // allow them to continue
                // (without this, the cursor stays a "NO" symbol
                e.Effect = DragDropEffects.All;
        }

        const string ALL_CAPS_PATTERN = "[A-Z]";

        static readonly Regex All_Caps_Regex =
           new Regex(ALL_CAPS_PATTERN);

        static bool AllUpperCase(string inputString)
        {         
            if (All_Caps_Regex.IsMatch(inputString))
            {
                return true;
            }
            return false;
        }

        private void _ClearListButton_Click(object sender, EventArgs e)
        {
            _SymbolsListBox.Items.Clear();
        }

        private void _SaveFileButton_Click(object sender, EventArgs e)
        {
            if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string location = _SaveFileDialog.FileName;

                StringBuilder stringBuilder = new StringBuilder();

                foreach (string symbol in _SymbolsListBox.Items)
                {
                    stringBuilder.AppendLine(symbol);
                }

                //write out the file symbols
                File.WriteAllText(location, stringBuilder.ToString());
            }
        }

        private void _SymbolsCountLabel_Click(object sender, EventArgs e)
        {

        }

        private void _SaveFileDialog_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void _SymbolsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void _SymbolPositionTextbox_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
